<template>
	<view>
		<!-- 搜索 -->
		<my-search bgcolor="#ff00ff" radius="9" @xxx="gotoSearch"></my-search>

		<!-- 分类 -->
		<view class="cate-container">
			<!-- 左侧一级分类 -->
			<scroll-view class="cate-left" scroll-y :style="{height: wh+'px'}">
				<!-- <view :class="['cate-left-item', active===i ? 'active':'' ]" @click="changeActive(i)"
					v-for="(item, i) in cateList" :key="i">
					{{item.cat_name}}
				</view> -->
				<!-- <view :class="{'cate-left-item': true, active: active === i }" @click="changeActive(i)"
					v-for="(item, i) in cateList" :key="i">
					{{item.cat_name}}
				</view> -->
				<view class="cate-left-item" :class="{active: active === i }" @click="changeActive(i)"
					v-for="(item, i) in cateList" :key="i">
					{{item.cat_name}}
				</view>
			</scroll-view>

			<!-- 右侧二级分类 -->
			<scroll-view scroll-y :scroll-top="scrollTop" class="cate-right" :style="{height: wh+'px'}">
				<!-- 二级分类item -->
				<view class="cate-right-item" v-for="(item, i) in cateLevel2" :key="i">
					<!-- 二级分类title -->
					<view class="cate-right-item-title">
						/{{item.cat_name}}/
					</view>
					<!-- 二级分类内容容器 -->
					<view class="cate-right-item-content">
						<!-- 二级分类每一项内容 -->
						<view class="cate-right-item-content-box" @click="gotoGoodsList(item2)" v-for="(item2, i2) in item.children"
							:key="i2">
							<!-- 二级分类图片 -->
							<image :src="item2.cat_icon" mode="widthFix"></image>
							<!-- 二级分类名称 -->
							<text>{{item2.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '../../mixin/tabbar-badge.js'

	export default {
		mixins: [badgeMix],
		data() {
			return {
				// 分类数据
				cateList: [],
				// 二级分类数据
				cateLevel2: [],

				// 一级分类选中状态
				active: 0,
				// 窗口可用高度
				wh: 0,
				// 滚动条距离顶部的距离
				scrollTop: 0
			};
		},
		// 发起网络请求
		onLoad() {
			// 获取窗口可用高度
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight - 50
			this.getCateList()
		},
		methods: {
			// 获取分类数据
			async getCateList() {
				const {
					data: res
				} = await uni.$http.get('/categories')
				// 数据异常判断
				if (res.meta.status !== 200) return uni.$showMsg()
				// 设置页面数据
				this.cateList = res.message
				// 二级分类
				this.cateLevel2 = this.cateList[this.active].children
			},

			// 改变索引的事件处理函数
			changeActive(i) {
				this.active = i
				// 设置二级分类数据
				this.cateLevel2 = this.cateList[this.active].children
				// 设置 scrollTop 值
				this.scrollTop = Math.random()
			},

			// 二级分类事件处理函数
			gotoGoodsList(cate) {
				uni.navigateTo({
					url: `/subpkg/goods_list/goods_list?cid=${cate.cat_id}`
				})
			},
			// 跳转到搜索也
			gotoSearch(e) {
				console.log('goto search', e)
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		}


	}
</script>

<style lang="scss">
	.cate-container {
		display: flex;

		.cate-left {
			width: 240rpx;

			.cate-left-item {
				display: flex;
				justify-content: center;
				align-items: center;
				height: 120rpx;
				background-color: #efefef;
				font-size: 24rpx;

				&.active {
					display: flex;
					justify-content: space-between;
					background-color: white;

					&::before {
						content: '';
						width: 6rpx;
						height: 60rpx;
						background-color: #c00000;
					}

					&::after {
						content: '';
						width: 6rpx;
						height: 60rpx;
						background-color: white;
					}
				}

			}
		}

		.cate-right {
			background-color: white;

			.cate-right-item {
				.cate-right-item-title {
					display: flex;
					justify-content: center;
					font-weight: bold;
					font-size: 24rpx;
					padding: 20rpx 0;
				}

				.cate-right-item-content {
					display: flex;
					flex-wrap: wrap;

					.cate-right-item-content-box {
						display: flex;
						flex-direction: column;
						padding: 20rpx;
						// border: 1px solid blue;
						justify-content: center;
						align-items: center;
						width: 33.33%;
						box-sizing: border-box;


						image {
							width: 100%;
							// border: 1px solid red;
						}

						text {
							font-size: 24rpx;
						}
					}
				}

			}
		}
	}
</style>